﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using System.Data;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{
    public class ACCStripAddressDbdata : SubscriptionDbDataBase
    {
        string _askStripNo = "0";
        List<string> stripTaskRecipeList;

        public ACCStripAddressDbdata(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
            stripTaskRecipeList = new List<string>();

        }

        public override void CreateSubscription()
        {
            try
            {

                opcServer.QuickAddMonitorItem(opcNodes.AccStripInfoAddress.GrpoupName,
                    opcNodes.AccStripInfoAddress.PublishTime,
                    opcNodes.AccStripInfoAddress.SampleTime,
                    opcNodes.AccStripInfoAddress.GetNodeAddress(),
                    opcNodes.AccStripInfoAddress.GetNodeDisplayName(),
                    SubscriptionHandler_AccStripInfoAddress);

                opcServer.QuickAddMonitorItem(opcNodes.AccStripInfoAskAddress.GrpoupName,
                    opcNodes.AccStripInfoAskAddress.PublishTime, 
                    opcNodes.AccStripInfoAskAddress.SampleTime,
                    opcNodes.AccStripInfoAskAddress.GetNodeAddress(), 
                    opcNodes.AccStripInfoAskAddress.GetNodeDisplayName(),
                    SubscriptionHandler_AccStripInfoAskAddress);

                opcServer.QuickAddMonitorItem(opcNodes.AccStripInfoRspAddress.GrpoupName,
                    opcNodes.AccStripInfoRspAddress.PublishTime,
                    opcNodes.AccStripInfoRspAddress.SampleTime,
                    opcNodes.AccStripInfoRspAddress.GetNodeAddress(),
                    opcNodes.AccStripInfoRspAddress.GetNodeDisplayName(),
                    SubscriptionHandler_AccStripInfoRspAddress);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("ACCStripAddressDbdata CreateSubscription", ex);
            }
        }

        private void SubscriptionHandler_AccStripInfoAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
            if (monitoredItem.DisplayName == "AccStripsInfoAsk")
            {
                bool ask = false;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {
                        Console.WriteLine("accData get ask");
                        Task.Run(() => HandleAskAccStrip());
                    }
                    else
                    {
                        Console.WriteLine("accData responsed");
                        opcServer.WriteValue("false", opcNodes.AccStripInfoAddress.AccStripsInfoRsp);
                    }
                }
            }
        }

        private void SubscriptionHandler_AccStripInfoAskAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
            if (monitoredItem.DisplayName == "Strip_No")
            {
                try
                {
                    _askStripNo = notification.Value.Value.ToString();
                }
                catch (Exception ex)
                {
                    _askStripNo = "0";
                    LogHelper.WriteLog("SubscriptionHandler_AccStripInfoAskAddress", ex);
                }
            }
        }

        private void SubscriptionHandler_AccStripInfoRspAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
        }

        private async Task HandleAskAccStrip()
        {
            await Task.Delay(500);

            //判断StripNo合法性
            if (!string.IsNullOrEmpty(_askStripNo) && _askStripNo != "0")
            {
                try
                {
                    DataTable stripTaskRecipeTable = STRIP_INFO.IsDegalityByStripNo(_askStripNo);
                    stripTaskRecipeList.Clear();
                    if (stripTaskRecipeTable != null && stripTaskRecipeTable.Rows.Count > 0)
                    {
                        stripTaskRecipeList.Add(stripTaskRecipeTable.Rows[0][RECIPE_INFO.fd_recipeNo].ToString());
                        stripTaskRecipeList.Add(Formats.ConvertUnit(stripTaskRecipeTable.Rows[0][RECIPE_INFO.fd_yeildStrength].ToString(), VswUnit.Yeild_Strength_L1,0));
                        stripTaskRecipeList.Add(Formats.ConvertUnit(stripTaskRecipeTable.Rows[0][RECIPE_INFO.fd_accPrPressure_1].ToString(), VswUnit.ACC_PR_PRESSURE_L1,0));
                        stripTaskRecipeList.Add(Formats.ConvertUnit(stripTaskRecipeTable.Rows[0][RECIPE_INFO.fd_accPrPressure_2].ToString(), VswUnit.ACC_PR_PRESSURE_L1,0));
                    }
                    else
                    {
                        stripTaskRecipeList.Add("-1");
                        stripTaskRecipeList.Add("-1");
                        stripTaskRecipeList.Add("-1");
                        stripTaskRecipeList.Add("-1");
                    }
                }

                catch (Exception ex)
                {
                    LogHelper.WriteLog("HandleAskAccStrip", ex);
                }

            }
            else
            {
                stripTaskRecipeList.Clear();
                stripTaskRecipeList.Add("-1");
                stripTaskRecipeList.Add("-1");
                stripTaskRecipeList.Add("-1");
                stripTaskRecipeList.Add("-1");
                LogHelper.WriteLog("HandleAskAccStrip write -1 unvaild parameter");
            }


            opcServer.WriteValues(stripTaskRecipeList, opcNodes.AccStripInfoRspAddress.GetNodeAddress());

            opcServer.WriteValue("true", opcNodes.AccStripInfoAddress.AccStripsInfoRsp);

        }

    }
}
